package sonic.game;
import sonic.game.util.Vector2D;

/**
 * Game Camera
 * 
 * Represents the position of the player's view
 * 
 * @author RGreenlees
 *
 */

public class GameCamera {

	public Vector2D location;
	
	public GameCamera()
	{
		location = new Vector2D(320,240);
	}
	
	/** Update the position of the camera according to the player's position */
	public void updateCam()
	{
		// Stay in place if the player is dead or missing
		if (Game.player != null && !Game.player.bDead)
		{
			// Leave a 40-pixel lag on the x and y axes rather than stick tight to the player
			if (Game.player.location.x > (location.x + 40))
			{
				location.x = Game.player.location.x - 40;
			}
			else if (Game.player.location.x < (location.x - 40))
			{
				location.x = Game.player.location.x + 40;
			}
			
			if (Game.player.location.y > (location.y + 40))
			{
				location.y = Game.player.location.y - 40;
			}
			else if (Game.player.location.y < (location.y - 40))
			{
				location.y = Game.player.location.y + 40;
			}
		}
		
		/* Don't allow the camera to stray outside the level boundaries. The camera 
		 * is 640 pixels wide and 320 tall, and the camera's location is the centre pixel
		 */
		
		/* If the camera's x location is less than 320, that means the left edge of the
		 * camera will be outside the map's leftmost boundaries, so lock the camera to location.x 320 
		 */
		if (location.x < 320)
		{
			location.x = 320;
		}
		
		// Same principal applies to the Y axis
		if (location.y < 240)
		{
			location.y = 240;
		}
		
		if (location.y + 240 > Game.currentMap.mapBoundsY)
		{
			location.y = Game.currentMap.mapBoundsY - 240;
		}
		
		if ((location.x + 320) > Game.currentMap.mapBoundsX)
		{
			location.x = Game.currentMap.mapBoundsX - 320;
		}
	}
}
